#include "LeagueCalendar.h"
#include "ZStream.h"
#include "OldMatch.h"

LeagueCalendar::LeagueCalendar(ServiceProvider* i_sp)
:m_sp(i_sp)
,m_currentTurn(0)
,m_teamList(0)
{
	assert(i_sp);
}

void LeagueCalendar::FillMatches(const size_t i_turnNumber, MatchDataVec& o_matches) const
{
	TurnGenerator::Couples c;
	m_turnGen.GetTurnNumber(i_turnNumber, c);

	o_matches.clear();
	o_matches.reserve(c.size());

	for(size_t i = 0; i < c.size(); ++i)
	{
		Team* team1 = (*m_teamList)[c[i].first];
		Team* team2 = (*m_teamList)[c[i].second];
		MatchData* match = new MatchData;
		match->Init(team1, team2);
		o_matches.push_back(match);
	}
}

void LeagueCalendar::Clear()
{
	m_turnGen.Clear();
	m_teamList = 0;
	m_currentTurn = 0;
}

void LeagueCalendar::Initialize(TeamVec& i_teamList)
{
	m_turnGen.Initialize(i_teamList.size());
	m_teamList = &i_teamList;
	m_currentTurn = 0;
}

ZStream& LeagueCalendar::Serialize(ZStream& o_stream) const
{
	DARIONLY(static const char* tag = "Calendar");
	NOTDARIONLY(static const char* tag = "LeagueCalendar");

	o_stream.WriteOpenTag(tag);
	o_stream.WriteAttribute("TurnNumber", m_currentTurn);
	m_turnGen.Serialize(o_stream);
	o_stream.WriteCloseTag(tag);
	return o_stream;
}

ZStream& LeagueCalendar::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();
	i_stream.ReadAttribute(m_currentTurn);
	m_turnGen.Deserialize(i_stream);
	i_stream.SkipLine();
	return i_stream;
}
